home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / STANDALO / BOUNCE_C / LINEBOX.C < prev    next >
Text File  |  1991-01-18  |  3KB  |  161 lines

  1. /*
  2.  * LineBox.c -- implementation of the LineBox class.
  3.  */
  4. #include "LineBox.h"
  5.  
  6. #include <QuickDraw.h>
  7. #include <MemoryMgr.h>
  8.  
  9.  
  10. /*
  11.  * randseed for my random number generator
  12.  */
  13. static unsigned long randseed = 1;
  14.  
  15. /*
  16.  * Initialize object and return FALSE if NewPtr call fails.
  17.  */
  18. Boolean LineBox::Init(int nlines, Rect *bounceBox, GrafPtr savePort)
  19. {
  20.     Rect bounds = *bounceBox;
  21.     int i;
  22.  
  23.     dtop = 3;
  24.     dbottom = 3;
  25.     dleft = 2;
  26.     dright = 6;
  27.     numlines = nlines;
  28.     port = savePort;
  29.  
  30.     top = bounds.top;
  31.     bottom = bounds.bottom;
  32.     left = bounds.left;
  33.     right = bounds.right;
  34.  
  35.     GetDateTime((long *)&randseed);
  36.  
  37.     if (lines = (Rect *)NewPtr((unsigned long)sizeof(Rect) * nlines)) {
  38.         RandomRect(&(lines[0]));
  39.         for (i = 1; i < numlines; ++i) {
  40.             lines[i] = lines[i - 1];
  41.             RecalcLine(i);
  42.         }
  43.         return TRUE;
  44.     }
  45.     else
  46.         return FALSE;
  47. }
  48.  
  49. void LineBox::DrawLine(int i)
  50. {
  51.     MoveTo(lines[i].left, lines[i].top);
  52.     LineTo(lines[i].right, lines[i].bottom);
  53. }
  54.  
  55. static int Randomize(int range)
  56. {
  57.     long rawResult;
  58.  
  59.     rawResult = rand();
  60.     if (rawResult < 0)
  61.         rawResult = - rawResult;
  62.     return (rawResult * range) / 32768;
  63. }
  64.  
  65. void LineBox::RandomRect(Rect *myRectPtr)
  66. {
  67.     myRectPtr->left = Randomize(right - left) + left;
  68.     myRectPtr->right = Randomize(right - left) + left;
  69.     myRectPtr->top = Randomize(bottom - top) + top;
  70.     myRectPtr->bottom = Randomize(bottom - top) + top;
  71. }
  72.  
  73. void LineBox::RecalcLine(int i)
  74. {
  75.     lines[i].top += dtop;
  76.     if ((lines[i].top < top) || (lines[i].top > bottom)) {
  77.         dtop *= -1;
  78.         lines[i].top += 2 * dtop;
  79.     }
  80.     lines[i].bottom += dbottom;
  81.     if ((lines[i].bottom < top) || (lines[i].bottom > bottom)) {
  82.         dbottom *= -1;
  83.         lines[i].bottom += 2 * dbottom;
  84.     }
  85.     lines[i].left += dleft;
  86.     if ((lines[i].left < left) || (lines[i].left > right)) {
  87.         dleft *= -1;
  88.         lines[i].left += 2 * dleft;
  89.     }
  90.     lines[i].right += dright;
  91.     if ((lines[i].right < left) || (lines[i].right > right)) {
  92.         dright *= -1;
  93.         lines[i].right += 2 * dright;
  94.     }
  95. }
  96.  
  97. void LineBox::Draw()
  98. {
  99.     Rect frame;
  100.     PenState savePen;
  101.     GrafPtr savePort;
  102.     int i;
  103.  
  104.     GetPort(&savePort);
  105.     SetPort(port);
  106.     GetPenState(&savePen);
  107.     PenNormal();
  108.     SetRect(&frame, left, top, right, bottom);
  109.     InsetRect(&frame, -BORDER, -BORDER);
  110. /* was:    FillRect(&frame, &black); black is an A5 qd global, no can do in cdevs */
  111.     EraseRect(&frame);
  112.     InvertRect(&frame);
  113.     PenMode(patXor);
  114.     for (i = 0; i < numlines; ++i) {
  115.         DrawLine(i);
  116.     }    
  117.     SetPenState(&savePen);
  118.     SetPort(savePort);
  119. }
  120.  
  121. void LineBox::Idle()
  122. {
  123.     GrafPtr savePort;
  124.     PenState savePen;
  125.     int i;
  126.  
  127.     GetPort(&savePort);
  128.     SetPort(port);
  129.     GetPenState(&savePen);
  130.     PenMode(patXor);
  131.     DrawLine(numlines - 1);
  132.     for (i = numlines - 1; i > 0; --i)
  133.         lines[i] = lines[i-1];
  134.     RecalcLine(0);
  135.     DrawLine(0);
  136.     SetPenState(&savePen);
  137.     SetPort(savePort);
  138. }
  139.  
  140. void LineBox::Close()
  141. {
  142.     if (lines)
  143.         DisposPtr(lines);
  144. }
  145.  
  146.  
  147. /*
  148.  *  rand - pseudo-random number generator
  149.  * (from THINK C 4.0 ANSI library)
  150.  */
  151.  
  152. static int
  153. rand()
  154. {
  155.     randseed = randseed * 1103515245 + 12345;
  156.     asm {
  157.         move.w    randseed,d0        ;  high word of long
  158.         andi.w    #0x7FFF,d0        ;  remove high bit
  159.     }
  160. }
  161.